<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Console\Commands;

use App\Models\Blog\Article;
use App\Models\Blog\ArticleContent;
use App\Models\Blog\Comment;
use App\Models\Common\FailedJob;
use App\Models\Expansion\Delete;
use App\Services\CommonService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class Trting extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'signature:globe {type}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        switch ( $this->argument('type') ) {//定时任务 分类
            case 1:
                $this->delete();//下载任务 隔天删除
                break;
            case 2:
                $this->email();//获取邮件
                break;
            case 3:
                $this->rPopEmail();//发送邮件
                break;
            case 4:
                $this->articleDelete();//30 天自动删除 软删除文章
                break;
            default:
                break;
        }
    }

    /**
     * 删除下载任务
     */
    private function delete()
    {
        $days  = (int) config('page.days');//获取 配置里面得隔几天删除
        $disk  = Storage::disk('storage');
        $where = function ( $query ) use ( $days ) {
            $query->where('created_at', '<', Carbon::now()->modify("-$days days"));
        };
        $data  = Delete::GetModel($where, [
            'id', 'name', 'uid', 'size', 'created_at',
        ]);
        try {

            foreach ( $data as $value ) {
                $del = Delete::DestroyModel($value->id);
                if ( ! $del ) throw new \Exception('删除失败');
                $true = $disk->exists($value->name);//获取文件是否存在
                if ( $true ) $disk->delete($value->name);//存在就删除
                errorStorage('del', $value->toArray(), $days . ' 天删除本地数据', $true ? 'success' : 'info', $true ? '自动清除文件' : '文件不存在或已删除');
            }
        } catch ( \Exception $exception ) {
            errorStorage('del', $exception->getTraceAsString(), $exception->getMessage(), 'error', '自动清除文件');
        }
    }

    /**
     * 获取邮箱里得邮件
     */
    private function email()
    {
        ( new CommonService() )->emailLaZaRd();
    }

    /**
     * 发送邮件
     */
    private function rPopEmail()
    {
        try {
            $key   = 'queues:default:reserved';
            $count = NULL;
            $where = function ( $query ) use ( $key ) {
                $query->where('key', $key);
                $query->where('status', 1);
                $query->where('static', 0);
            };
            $jobs  = FailedJob::AllModel($where, [], [ 'id', 'key', 'content', 'count', 'status', 'static' ]);
            foreach ( $jobs as $job ) {
                $count   = $job;
                $failure = emailMail(json_decode($job->content, TRUE));
                if ( ! $failure ) {
                    $data[ 'count' ]  = $job->count + 1;
                    $data[ 'static' ] = 2;
                    $job->update($data);
                }
                else {
                    $data[ 'static' ] = 1;
                    $job->update($data);
                }
            }
        } catch ( \Exception $exception ) {
            if ( $count ) {
                $data[ 'count' ]  = $count->count + 1;
                $data[ 'static' ] = 2;
                $count->update($data);
            }
            errorStorage('code', $exception->getTraceAsString(), $exception->getMessage(), 'error', '发送邮件');
        }
    }

    /**
     * 30 天自动删除文章
     */
    private function articleDelete()
    {
        $where   = function ( $query ) {
            $query->where('deleted_at', '<=', Carbon::now()->subMonth());
            $query->where('static', 1);
        };
        $select  = [ 'id', 'user_id', 'title', 'static' ];
        $article = Article::GetModel($where, [], $select);
        unset($where);
        try {
            DB::beginTransaction();
            foreach ( $article as $value ) {
                errorStorage('articleDel', $value->toArray(), '30天删除回收站', 'success', '自动删除30天的回收站');
                $value->static = 3;
                $value->save();
            }
            DB::commit();
        } catch ( \Exception $exception ) {
            DB::rollBack();
            errorStorage('articleDel', $exception->getTraceAsString(), $exception->getMessage(), 'error', '自动删除30天的回收站');
        }
    }

}
